package com.b00t.client.view;

import java.util.ArrayList;

import com.b00t.shared.OrderDetails;
import com.b00t.shared.ProductDetails;
import com.b00t.shared.ProductImages;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class FulfillmentView extends Composite {

	/**
	 * Entry point classes define <code>onModuleLoad()</code>.
	 */
	private HorizontalPanel title = new HorizontalPanel();
	private HorizontalPanel img = new HorizontalPanel();
	private TextBox purchaseOrder = new TextBox();
	private ArrayList<String> orderNos = new ArrayList<String>();
	private FlexTable fd = new FlexTable();
	private HorizontalPanel oP = new HorizontalPanel();
	private Label dummy = new Label();
	private Grid main = new Grid(1, 2);
	private FlowPanel left = new FlowPanel();
	private FlowPanel right = new FlowPanel();

	public FulfillmentView() {

		final HTML selectOrder = new HTML("<b>Select Purchase Order</b>");
		main.setStyleName("mainCls");
		
		main.setWidget(0, 0, left);
		main.getCellFormatter().addStyleName(0, 0, "left");
		main.getCellFormatter().addStyleName(0, 1, "right");
		main.setWidget(0, 1, right);

		dummy.setStyleName("dummy");
		title.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		title.add(selectOrder);
		title.add(purchaseOrder);
		title.setStyleName("sPO");

		selectOrder.setHorizontalAlignment(HTML.ALIGN_RIGHT);
		selectOrder.setStyleName("sText");

		left.add(title);
		left.add(dummy);

		purchaseOrder.addKeyUpHandler(new KeyUpHandler() {

			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					dummy.removeFromParent();
					showImages();
				}
			}
		});
		initWidget(main);
	}

	private Anchor buildImage() {
		ProductImages images = new ProductImages();
		final Anchor anchor = new Anchor();
		final Image image = new Image();
		image.setUrl(images.getImageUrl());
		image.setWidth("90px");
		image.setHeight("90px");
		image.setStyleName("img");
		anchor.setHref("#");
		anchor.getElement().appendChild(image.getElement());

		anchor.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				showOrderProductDetails();
				showOrderDetails();
				showFulfilmentDetails();
			}
		});
		return anchor;
	}

	private void showOrderProductDetails() {
		oP.clear();
		oP.setWidth("100%");
		oP.setStyleName("oPro");
		oP.add(showOrderDetails());
		oP.add(showProductDetails());

		oP.setCellWidth(oP.getWidget(0), "50%");
		oP.setCellWidth(oP.getWidget(1), "50%");

		left.add(oP);
	}

	private FlexTable showProductDetails() {
		final FlexTable pd = new FlexTable();
		pd.setCellPadding(0);
		pd.setCellSpacing(0);
		pd.setWidth("100%");

		ProductDetails product = new ProductDetails();

		pd.setText(0, 0, "Product Details");
		pd.setText(1, 0, product.getProduct());
		pd.setText(2, 0, product.getCompany());
		pd.setText(3, 0, product.getModel());

		pd.getCellFormatter().addStyleName(0, 0, "tblHead");
		pd.getCellFormatter().addStyleName(1, 0, "tblMdlLftRgt");
		pd.getCellFormatter().addStyleName(2, 0, "tblMdlLftRgt");
		pd.getCellFormatter().addStyleName(3, 0, "tblBtmLftRgt");

		return pd;
	}

	private FlexTable showOrderDetails() {
		final FlexTable od = new FlexTable();
		od.setCellPadding(0);
		od.setCellSpacing(0);
		od.setWidth("100%");
		od.setStyleName("ordrDtl");

		OrderDetails order = new OrderDetails();

		od.setText(0, 0, "Order Details");
		od.setText(1, 0, "By");
		od.setText(1, 1, order.getName());
		od.setText(2, 0, "On");
		od.setText(2, 1, order.getDate());
		od.setText(3, 0, "Quantity");
		od.setText(3, 1, order.getQuantity());

		od.getCellFormatter().addStyleName(0, 0, "tblHead");
		od.getCellFormatter().addStyleName(1, 0, "tblMdlLft");
		od.getCellFormatter().addStyleName(2, 0, "tblMdlLft");
		od.getCellFormatter().addStyleName(3, 0, "tblBtmLft");
		od.getCellFormatter().addStyleName(1, 1, "tblMdlRgt");
		od.getCellFormatter().addStyleName(2, 1, "tblMdlRgt");
		od.getCellFormatter().addStyleName(3, 1, "tblBtmRgt");

		od.getFlexCellFormatter().setColSpan(0, 0, 2);

		return od;
	}

	private void showFulfilmentDetails() {
		fd.clear();
		fd.setCellPadding(0);
		fd.setCellSpacing(0);
		fd.setWidth("50%");

		final TextBox received = new TextBox();
		final TextBox accepted = new TextBox();
		final TextBox rejected = new TextBox();
		final TextBox returned = new TextBox();

		fd.setText(0, 0, "Fulfillment");
		fd.setText(1, 0, "Received");
		fd.setWidget(1, 1, received);
		fd.setText(2, 0, "Accepted");
		fd.setWidget(2, 1, accepted);
		fd.setText(3, 0, "Rejected");
		fd.setWidget(3, 1, rejected);
		fd.setText(4, 0, "Returned");
		fd.setWidget(4, 1, returned);

		fd.getCellFormatter().addStyleName(0, 0, "tblHead");
		fd.getCellFormatter().addStyleName(1, 0, "tblMdlLft");
		fd.getCellFormatter().addStyleName(2, 0, "tblMdlLft");
		fd.getCellFormatter().addStyleName(3, 0, "tblMdlLft");
		fd.getCellFormatter().addStyleName(4, 0, "tblBtmLft");
		fd.getCellFormatter().addStyleName(1, 1, "tblMdlRgt");
		fd.getCellFormatter().addStyleName(2, 1, "tblMdlRgt");
		fd.getCellFormatter().addStyleName(3, 1, "tblMdlRgt");
		fd.getCellFormatter().addStyleName(4, 1, "tblBtmRgt");

		fd.getFlexCellFormatter().setColSpan(0, 0, 2);
		right.add(fd);
	}

	private void showImages() {

		final String order = purchaseOrder.getText().trim();
		purchaseOrder.setFocus(true);

		if (!order.matches("^[0-9\\.]{1,10}$")) {
			Window.alert("'" + order + "' is not a valid order number");
			purchaseOrder.selectAll();
			return;
		}

		if (orderNos.contains(order)) {
			return;
		} else {
			img.clear();
			img.setStyleName("pImg");
			img.add(buildImage());
		}

		orderNos.add(order);
		left.add(img);
	}

}
